package com.example.newsenior;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.example.newsenior.databinding.ActivityMainBinding;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private NavigationBarView.OnItemSelectedListener navListener = new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.navigation_home:
                    //binding.tvMessage.setText(R.string.title_home);
                    binding.viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
                    //binding.tvMessage.setText(R.string.title_dashboard);
                    binding.viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_notification:
                    //binding.tvMessage.setText(R.string.title_notification);
                    binding.viewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //加载xml菜单资源
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_settings:
                showSnackbarMsg("123" + item.getTitle());
                return true;
            default:
                return false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //设置自定义Toolbar
        setSupportActionBar(binding.toolbar);  //界面主题设为NoActionBar

        //配置ViewPager的适配器
        final  MainViewPageAdapter viewPageAdapter = new MainViewPageAdapter(this);
        binding.viewPager.setAdapter(viewPageAdapter);
        binding.viewPager.setOffscreenPageLimit(viewPageAdapter.getItemCount() - 1);
        binding.viewPager.setUserInputEnabled(false);

        //注册滑动事件监听
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                binding.navigation.getMenu().getItem(position).setCheckable(true);
            }
        });

        //底部导航监听器
        binding.navigation.setOnItemSelectedListener(navListener);
        bindNavigationDrawer();
    }

    private void bindNavigationDrawer() {
        final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, binding.drawerLayout,binding.toolbar,
                R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        binding.drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        binding.navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.navigation_home:
                        binding.viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_dashboard:
                        binding.viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_notification:
                        binding.viewPager.setCurrentItem(2);
                        return true;
                }

                return false;
            }
        });
    }


    public void onFABClick(View view) {
        showSnackbarMsg("FAB button click");
    }

    private void showSnackbarMsg(String message) {
        Snackbar snackbar = Snackbar.make(binding.coordinatorLayout,message,Snackbar.LENGTH_LONG);
        snackbar.setAction("ok", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                snackbar.dismiss();
            }
        });
        snackbar.show();
    }


    private static class MainViewPageAdapter extends FragmentStateAdapter{
        public MainViewPageAdapter(@NonNull FragmentActivity fragmentActivity){
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position){
                case 0:
                    return HomeFragment.newInstance();
                case 1:
                    return DashboardFragment.newInstance();
                case 2:
                    return NotificationFragment.newInstance();
            }
            return HomeFragment.newInstance();
        }


        @Override
        public int getItemCount() {
            return 3;
        }
    }
}